import pandas as pd
from math import sqrt, pow
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('expand_frame_repr',False)
#['mmsi', 'longitude', 'latitude', 'cog', 'sog', 'aistime', 'Unixtime', 'traj_segment']

threshold = 0.001
qualify_list = list() #处理好的数据
disqualify_list = list() #未处理好的数据

def point2LineDistance(point_a, point_b, point_c):
    """
    计算点a到点b c所在直线的距离
    :param point_a:
    :param point_b:
    :param point_c:
    :return:
    """
    # 首先计算b c 所在直线的斜率和截距
    if point_b[1] == point_c[1]:
        return 9999999
    #斜率
    slope = (point_b[2] - point_c[2]) / (point_b[1] - point_c[1])
    #截距
    intercept = point_b[2] - slope * point_b[1]

    # 计算点a到b c所在直线的距离
    distance = abs(slope * point_a[1] - point_a[2] + intercept) / sqrt(1 + pow(slope, 2))
    return distance
def diluting(point_list):
    max_distance_index, max_distance = 0, 0
    for index, point in enumerate(point_list):
        if index in [0, len(point_list) - 1]:
            continue
        distance = point2LineDistance(point, point_list[0], point_list[-1])
        if distance > max_distance:
            max_distance_index = index
            max_distance = distance
    if max_distance < threshold:
        qualify_list.append(point_list[-1])
        qualify_list.append(point_list[0])
    else:
        # 将曲线按最大距离的点分割成两段
        sequence_a = point_list[:max_distance_index]
        sequence_b = point_list[max_distance_index:]
        for sequence in [sequence_a, sequence_b]:
            if len(sequence) < 2 and sequence == sequence_b:
                qualify_list.extend(sequence[::-1])
            else:
                disqualify_list.append(sequence)

def dp(data):
    MMSIDATA = data['mmsi']
    SHIPSMMSI = list(set(MMSIDATA))
    shipcount = len(SHIPSMMSI)
    for i in range(shipcount):
        mmsi = SHIPSMMSI[i]
        ONESHIP = data.iloc[:, :][data[data.T.index[1]] == mmsi]  # 对于这艘船
        TRAJ_SEGMENT = list(set(ONESHIP["traj_segment"]))  # 这艘船轨迹段的set集合
        trajcount = len(TRAJ_SEGMENT)  # 这艘船轨迹段的数量
        for j in range(trajcount):
            ONETRAJ = ONESHIP.iloc[:, :][ONESHIP[ONESHIP.T.index[9]] == j]  # 对于这条轨迹
            onetrajlens = len(ONETRAJ)
            onetrajdata = [[] for k in range(int(onetrajlens))]
            mmsi = ONETRAJ['mmsi'].reset_index(drop=True)
            longitude = ONETRAJ['longitude'].reset_index(drop=True)
            latitude = ONETRAJ['latitude'].reset_index(drop=True)
            cog = ONETRAJ['cog'].reset_index(drop=True)
            sog = ONETRAJ['sog'].reset_index(drop=True)
            # aistime = ONETRAJ['aistime'].reset_index(drop=True)
            Unixtime = ONETRAJ['Unixtime'].reset_index(drop=True)
            traj_segment = ONETRAJ['traj_segment'].reset_index(drop=True)
            for m in range(int(onetrajlens)):
                onetrajdata[m].append(mmsi[m])
                onetrajdata[m].append(longitude[m])
                onetrajdata[m].append(latitude[m])
                onetrajdata[m].append(cog[m])
                onetrajdata[m].append(sog[m])
                # onetrajdata[m].append(aistime[m])
                onetrajdata[m].append(Unixtime[m])
                onetrajdata[m].append(traj_segment[m])
            diluting(onetrajdata)
            while len(disqualify_list) > 0:
                diluting(disqualify_list.pop())
    # shipdata = pd.DataFrame(qualify_list, columns=['mmsi', 'longitude', 'latitude', 'cog', 'sog', 'aistime', 'Unixtime','traj_segment'])
    shipdata = pd.DataFrame(qualify_list, columns=['mmsi', 'longitude', 'latitude', 'cog', 'sog', 'Unixtime','traj_segment'])
    return shipdata

# shiplist = [[] for j in range(len(data))]
# mmsi=data['mmsi']
# longitude=data['longitude']
# latitude=data['latitude']
# cog=data['cog']
# sog=data['sog']
# aistime=data['aistime']
# Unixtime=data['Unixtime']
# traj_segment=data['traj_segment']
# for i in range(len(data)):
#     shiplist[i].append(mmsi[i])
#     shiplist[i].append(longitude[i])
#     shiplist[i].append(latitude[i])
#     shiplist[i].append(cog[i])
#     shiplist[i].append(sog[i])
#     shiplist[i].append(aistime[i])
#     shiplist[i].append(Unixtime[i])
#     shiplist[i].append(traj_segment[i])

